﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Minecraft_2D.Formulaires.Menu
{
    public partial class Pause : Form
    {
        #region Attributs
        private Minecraft2D _game;
        private static bool _created;
        private static Stream _stream;
        private static string _mapName;
        //private Keys[] _keys;
        #endregion

        #region Constructeur
        public Pause(Minecraft2D game)
        {
            InitializeComponent();
            _game = game;
        }
        #endregion

        #region Proprietes
        #endregion

        #region Methodes
        public static void Save(Map map)
        {
            if (!_created)
            {
                _mapName = (_mapName == null) ? "Minecraft" : _mapName;
                if (Directory.Exists(@"Maps\" + _mapName))
                {
                    int i = 1;
                    string mapName = _mapName;
                    while (Directory.Exists(@"Maps\" + mapName + (" - " + i)))
                    {
                        ++i;
                    }
                    _mapName += (" - " + i);
                }
                Directory.CreateDirectory(@"Maps\" + _mapName + @"\");
                File.Create(@"Maps\" + _mapName + @"\" + _mapName + ".mc2d").Close(); ;
            }

            XmlSerializer xml = new XmlSerializer(typeof(Map));
            using (StreamWriter wr = new StreamWriter(@"Maps\" + _mapName + @"\" + _mapName + ".mc2d"))
            {
                xml.Serialize(wr, map);
            }
            _created = true;
        }

        public void Open(ref Map map)
        {
            OpenFileDialog open = new OpenFileDialog() { InitialDirectory = @"Maps", Filter = "MineCraft Map|*.mc2d", Multiselect = false };
            if (open.ShowDialog() == DialogResult.OK)
            {
                _stream = open.OpenFile();
                XmlSerializer xml = new XmlSerializer(typeof(Map));
                using (StreamReader wr = new StreamReader(_stream))
                {
                    map = (Map)xml.Deserialize(wr);
                }
                _mapName = open.FileName;
                textBox1.Text = _mapName;
                _stream.Close();
                _game.Build();
            }
        }

        public new void ShowDialog()
        {
            label1.Text = Properties.Settings.Default["Btn_Haut"].ToString();
            label2.Text = Properties.Settings.Default["Btn_Bas"].ToString();
            label3.Text = Properties.Settings.Default["Btn_Gauche"].ToString();
            label4.Text = Properties.Settings.Default["Btn_Droite"].ToString();
            textBox1.Text = _mapName;
            base.ShowDialog(_game);
        }
        #endregion

        #region Controles
        private void Pause_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)27)
                this.Close();
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            _mapName = textBox1.Text;
            _created = true;
            Save(_game.Map);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Map map = _game.Map;
            Open(ref map);
            _created = true;
            _game.Map = map;
        }
        #endregion

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            _created = false;
        }
    }
}
